import modelExtend from 'dva-model-extend'
import { detail } from 'services/financialPaymentBill'
import { lwjPageModel } from 'utils/model'
import { routerRedux } from 'dva/router';
import pathToRegexp from 'path-to-regexp';

export default modelExtend(lwjPageModel, {
    namespace: 'financialPaymentBillDetail',
    state: {
        currentItemDetail: {}
    },
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname }) => {
                const match = pathToRegexp('/financialPaymentBill/detail/:id').exec(pathname);
                if (match) {
                    const id = match[1];
                    dispatch({
                        type: 'detail',
                        payload: {
                            id: id
                        }
                    })
                }
            })
        }
    },
    effects: {
        * detail({
            payload
        }, { select, call, put }) {
            const data = yield call(detail, payload.id);
            yield put({
                type: 'detailSuccess',
                payload: {
                    currentItemDetail: data.result
                }
            })
        }
    },
    reducers: {
        detailSuccess(state, { payload }) {
            return {
                ...state,
                ...payload
            }
        }
    }
})
